//
//  WatchDetailVC.m
//  Blank
//
//  Created by whg－54 on 15/9/28.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "WatchDetailVC.h"
#import "AFNetWorkingGet.h"
#import <UIImageView+WebCache.h>
#import "WatchDetailUpHalfView.h"
#import "BrandDisplayModel.h"
@interface WatchDetailVC ()<UIWebViewDelegate>
@property (nonatomic , strong)UIWebView *webView;
@property (nonatomic , strong)UIImageView *picView;
@property (nonatomic , strong)NSDictionary *contenDic;
@property (nonatomic , strong)WatchDetailUpHalfView *upHalfView;
@property (nonatomic ,strong)UIActivityIndicatorView *indicator;
@end

@implementation WatchDetailVC
- (void) viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;

    [AFNetWorkingGet popAlertIfNoInter:self];
    
    
    //KVO
    [self.webView.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    
    if ([NightModel getColorModel]) {
        
        self.upHalfView.backgroundColor = [UIColor lightGrayColor];
        

    }
    else{
        self.upHalfView.backgroundColor = [UIColor whiteColor];

     }
    
    
    
    
    NSLog(@"%@" , self.popID);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonBack setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    buttonBack.frame = CGRectMake(0, 0, 20, 20);
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonBack];
    
    [buttonBack addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    /**
     设置webView
     */
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,300*Height, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 300*Height - self.tabBarController.tabBar.frame.size.height*2)];
    self.webView.delegate = self;
 
    [self.view addSubview:self.webView];
    /**
     *  设置upHalfView
     */
    self.upHalfView = [[WatchDetailUpHalfView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300*Height)];
    [self.view addSubview:self.upHalfView];
    

    /**
     *  数据解析
     */
    NSString *urlStr = [NSString stringWithFormat:@WatchDetailURL , self.popID];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [self.webView loadRequest:request];
    
    NSString *str = [NSString stringWithFormat:@WatchDetailAFURL , self.popID];
    
    [AFNetWorkingGet GETWithAFNByURL:str completion:^(id result) {
        self.contenDic = result;
        BrandDisplayModel *model = [[BrandDisplayModel alloc]init];
        [model setValuesForKeysWithDictionary:self.contenDic];
        self.upHalfView.model = model;
    }];
    
    /**
     *  小菊花
     */
    self.indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.indicator.frame = CGRectMake(0, 0, 50*Windth, 50*Height);
    self.indicator.center = CGPointMake(self.view.center.x, self.view.center.y - 30*Height);
    self.indicator.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.indicator];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
}
#pragma mark - 点击按钮返回
- (void)back:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
    
}


#pragma mark - kvo方法
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"%@" , change);
    CGPoint new = [[change objectForKey:@"new"]CGPointValue];
    if (new.y > 0 && new.y < 300*Height) {
        self.upHalfView.frame = CGRectMake(0, 0 - new.y, self.upHalfView.frame.size.width, self.upHalfView.frame.size.height);
        self.webView.frame = CGRectMake(0, 300*Height - new.y, self.webView.frame.size.width, [UIScreen mainScreen].bounds.size.height);
    }
    else if(new.y >=  300*Height)
    {
        self.upHalfView.frame = CGRectMake(0, -310*Height, self.upHalfView.frame.size.width, self.upHalfView.frame.size.height);

        self.webView.frame = CGRectMake(0, 0, self.webView.frame.size.width, [UIScreen mainScreen].bounds.size.height- self.tabBarController.tabBar.frame.size.height*2);

    }
    else{
        
        self.upHalfView.frame = CGRectMake(0, 0, self.upHalfView.frame.size.width, self.upHalfView.frame.size.height);
        self.webView.frame = CGRectMake(0, 300*Height, self.webView.frame.size.width, [UIScreen mainScreen].bounds.size.height - self.tabBarController.tabBar.frame.size.height*2);
        
    }
}
#pragma mark - webView加载
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.indicator stopAnimating];
    if ([NightModel getColorModel]) {
        
        //        self.webView.backgroundColor = [UIColor lightGrayColor];
        //        self.webView.scrollView.alpha = 0.8;
        //        self.webView.opaque = NO;
        //webview字体颜色
        [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
        //页面背景色
        [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='gray'"];
    }
    else{
        //        self.webView.opaque = YES;
        //        self.webView.scrollView.alpha = 1;
        //        self.webView.backgroundColor = [UIColor whiteColor];
        
        //webview字体颜色
        [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];
        //页面背景色
        [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='white'"];
        
    }

    
}

#pragma mark - webVIew完成加载
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [self.indicator startAnimating];
}

#pragma mark - 视图将要消失
- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.webView.scrollView removeObserver:self forKeyPath:@"contentOffset"];
}

#pragma mark - memory
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
